﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class RepairModelProblemListCtrl : BaseListControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public RepairModelProblems ModelProblems
        {
            get { return (RepairModelProblems)SourceObject; }
            set { SourceObject = value; }
        }

        public void UpdateAll()
        {
            if (ModelProblems == null)
                return;
            String oldFilter = ModelProblems.Filter;
            ModelProblems.Rewind();
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                int id = (int)grid.DataKeys[i];
                
                ModelProblems.Filter = RepairModelProblems.ColumnNames.ID + "=" + id.ToString();
                if (ModelProblems.RowCount > 0)
                {
                    TextBox txtRPP = item.FindControl("txtRPP") as TextBox;
                    TextBox txtStandardPrice = item.FindControl("txtStandardPrice") as TextBox;
                    TextBox txtSpecialPrice = item.FindControl("txtSpecialPrice") as TextBox;

                    if(ModelProblems.s_RPP != txtRPP.Text)
                        ModelProblems.s_RPP = txtRPP.Text;
                    if (ModelProblems.s_SpecialPrice != txtSpecialPrice.Text)
                        ModelProblems.s_SpecialPrice = txtSpecialPrice.Text;
                    if (ModelProblems.s_StandardPrice != txtStandardPrice.Text)
                        ModelProblems.s_StandardPrice = txtStandardPrice.Text;
                }
            }

            ModelProblems.Save();
            ModelProblems.Filter = oldFilter;
        }

        public bool ReadOnly
        {
            get { return (bool)ViewState[ClientID + "_ReadOnly"]; }
            set { ViewState[ClientID + "_ReadOnly"] = value; }
        }

        protected override void grid_OnItemCreated(object sender, DataGridItemEventArgs e)
        {
            base.grid_OnItemCreated(sender, e);

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
            {
                TextBox txtRPP = e.Item.FindControl("txtRPP") as TextBox;
                TextBox txtStandardPrice = e.Item.FindControl("txtStandardPrice") as TextBox;
                TextBox txtSpecialPrice = e.Item.FindControl("txtSpecialPrice") as TextBox;

                Label lblRPP = e.Item.FindControl("lblRPP") as Label;
                Label lblStandardPrice = e.Item.FindControl("lblStandardPrice") as Label;
                Label lblSpecialPrice = e.Item.FindControl("lblSpecialPrice") as Label;

                if (lblRPP != null)
                {
                    lblRPP.Visible = lblSpecialPrice.Visible = lblStandardPrice.Visible = ReadOnly;
                    txtRPP.Visible = txtSpecialPrice.Visible = txtStandardPrice.Visible = !ReadOnly;
                }
            }
        }
    }
}